fix cumulative_distribution_function - #110
Conversation
|
Thanks for the fix. Could you add the example from #111 as a test? Your correct example appears to me to be correct, i.e., the final value in the CDF should be 1, so we should have a test that that condition is met. I have to think through the searchsorted(..., right) logic more - your explanation makes sense, but I haven't touched this code in a decade and want to take a minute to think it over. Having a test case or two in place would really help. |
|
I'll see what i can do about the tests! gives gives Using |
|
I was just about to look into the tests when i found this comment in I don't know what this is about, but considering that it mentions the cumulative distribution function, whoever made these fixes may have fixed the wrong thing. I have added a test to the cumulative_distribution_function (and the pdf for good measure). But using the fixed CDF it now fails three other tests. |
|
Thanks for the explanations. It's entirely possible there were errors similar to what you've found here in the original plfit code that we incorrectly replicated. I'm going to start a separate PR to get CI running so we can see these tests... |
|
Could you rebase against master? That will get CI running and will show the test failures. I'd believe that you've corrected a long-standing error in the code, in which case we need to update the tests, but I don't want to jump to that conclusion without walking through the math myself or at least getting one other to review. I don't have time for that at the moment. Curiously, the failures I see are in the lognormal and exponential tests. I don't see immediately why those fits would change so much. The third test failure is this: which looks like it might be exactly the off-by-one error for discrete (integer) valued data. |
added +1 to cumulative_distribution_function
Co-authored-by: Adam Ginsburg <keflavich@gmail.com>
|
Sorry it's taken me a while to get to this, I've been meaning to review it for a while! I think the fix to the CDF calculation indeed does fix an old bug. I'm not incredibly familiar with the For the I'll note that I initially started by looking at the Kolmogorov-Smirnov distance, which gives the following results: ie. the opposite conclusion as above. That being said, since the KSD is dependent on the actual CDF calculation, it isn't a reliable way to calculate whether the CDF calculation itself is correct or not. The likelihood is calculated from the PDF instead, making it more suitable for this comparison. This argument above only addresses the failure of the power law fitting test, though I think this exemplifies why caching the values of the loglikelihood ratios for distributions that don't actually fit the dataset isn't a good way to benchmark accuracy or consistency. I can't see any particular reason why the exponential or lognormal distributions fail while the others pass, other than just random chance. Assuming we're convinced about this being a fix, there are a few things to do before merging this:
@EliasL if you're busy and want me to make these changes, please let me know. Thanks for all of your work! :) |
|
Hello, Your comments inspired me to make a propper test of the CDF, and this revealed that there was an even bigger issue in the KS distance itself. I found a source which describes the KS distance more explicitly than what i have seen before: Here we can recognize what the old code did in D- (i-1/n) and my new code in D+ ((i)/n). Note that they use one-indexing on i. It turns out that the real KS statistic uses both the old version (that i claimed was wrong), and my new proposal! In the current KS code, D+ and D- are just doing (max(abs(CDF_diff)) with extra steps. Here is what is actually supposed to happen: When we have an empirical CDF (CDF_E), like for example
The KS statistic suggests that we should try both, and keep whichever gives the largest distance. There is an analytical expression for the expected KS distance from a model given a certain data set size
which works out to be
(To be confirmed...) Using a sample size of I know this is a lot, and I have not quite digested everything myself, but if you could take some time to see if you agree with me, I then suggest that we close this branch, and i'll fork the current head and try to suggest a fix to the KS function itself. (But I am not good with git, and open to any suggestions you might prefer) |




Fixed a missing +1 to cumulative_distribution_function.
#111